﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities.PBKT;
using Entities.Admin;

namespace WindowsForms.PBKT
{
	public partial class FormDoiTuyenThem : Form
	{
		DoiTuyen doiTuyen = null;
		List<CauThu> dsCauThu = null;

		public FormDoiTuyenThem(DoiTuyen dt)
		{
			InitializeComponent();
			doiTuyen = dt;
			if (doiTuyen != null)
				dsCauThu = CauThu.Objects.Find(c => c.MaDT == dt.MaDT).ToList();

		}

		private void FormDoiTuyenThem_Load(object sender, EventArgs e)
		{
			cbbGiaiDau.DataSource = GiaiDau.Objects.All().ToList();
			cbbGiaiDau.DisplayMember = "TenGiaiDau";
			cbbGiaiDau.ValueMember = "MaGiaiDau";
			dgvCauThu.AutoGenerateColumns = false;
			if (doiTuyen != null)
			{
				txtMaDT.Enabled = false;
				txtMaDT.Text = doiTuyen.MaDT;
				txtTenDT.Text = doiTuyen.TenDT;
				txtTruongDoan.Text = doiTuyen.TruongDoan;
				for (int i = 0; i < cbbGiaiDau.Items.Count; i++)
				{
					GiaiDau gd = cbbGiaiDau.Items[i] as GiaiDau;
					if (gd.MaGiaiDau == doiTuyen.MaGD)
					{
						cbbGiaiDau.SelectedIndex = i;
						break;
					}
				}
				LoadDSCauThu(dsCauThu);
			}


		}
		void LoadDSCauThu(List<CauThu> list)
		{
			dgvCauThu.DataSource = list;

		}
		private void btnReset_Click(object sender, EventArgs e)
		{
			txtMaDT.Text = "";
			txtTenDT.Text = "";
			txtTruongDoan.Text = "";
		}
		private DoiTuyen GetDoiTuyen()
		{
			DoiTuyen dt = new DoiTuyen();
			if (doiTuyen != null)
			{
				dt.MaDT = doiTuyen.MaDT;
			}
			else dt.MaDT = txtMaDT.Text;
			dt.TenDT = txtTenDT.Text;
			dt.TruongDoan = txtTruongDoan.Text;
			GiaiDau gd = cbbGiaiDau.SelectedItem as GiaiDau;
			if (dt.MaDT.Length > 3 && gd != null && dt.MaDT != null)
			{
				dt.MaGD = gd.MaGiaiDau;
				return dt;
			}
			else
			{
				MessageBox.Show("Mã đội tuyển phải lớn hơn 3 ký tự hoặc chưa chọn giải đấu");
				return null;

			}
		}
		private void btnLuu_Click(object sender, EventArgs e)
		{
			DoiTuyen dt = GetDoiTuyen();
			if (dt != null)
				if (DoiTuyen.Objects.InsertOrUpdate(dt))
					MessageBox.Show("Lưu thành công");
				else
					MessageBox.Show("Lưu không thành công");
		}

		private void btnThoat_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void dgvCauThu_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{
			try
			{
				if (e.ColumnIndex == 4)
				{
					CauThu cThu = CauThu.Objects.GetById(dgvCauThu.Rows[e.RowIndex].Cells[0].Value);
					if (CauThu.Objects.Delete(cThu))
					{
						MessageBox.Show("Xóa thành công");
						//LoadDSCauThu(CauThu.Objects.Find(c=>c.MaDT=);
					}
					else
						MessageBox.Show("Xóa không thành công");
				}
				if (e.ColumnIndex == 5)
				{

					FormCauThuThem frm = new FormCauThuThem(CauThu.Objects.GetById(dgvCauThu.Rows[e.RowIndex].Cells[0].Value));
					frm.ShowDialog();
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}

		}


		private void thêmCầuThủToolStripMenuItem_Click(object sender, EventArgs e)
		{
			FormCauThuThem frm = new FormCauThuThem(txtMaDT.Text);
			frm.ShowDialog();
			dgvCauThu.DataSource = CauThu.Objects.Find(c => c.MaDT == txtMaDT.Text).ToList();
		}


	}
}
